#include "config.h"

#include <QDebug>

CONFIG::CONFIG()
{
    filep = "./config.json";

}

CONFIG::CONFIG(QString file)
{
    filep = file;
}

//从文件中读取根句柄
void CONFIG::getJson_from_file()
{
    QFile file(filep);

    if(file.open(QIODevice::ReadOnly) == false)
    {
        //文件打开失败，尝试创建文件并打开
        if(file.open(QIODevice::NewOnly) == false)
        {
            qDebug()<<"open config.json NewOnly | ReadOnly faild";
            return;
        }
    }

    QByteArray Jsondata = file.readAll();

    QJsonDocument Jsondoc = QJsonDocument::fromJson(Jsondata);

    config = Jsondoc.object();

    file.close();

    return;
}

//直接将根句柄写入文件中
void CONFIG::updataJson_to_File(QJsonDocument Jsondoc)
{
    QFile file(filep);
    if(file.open(QIODevice::WriteOnly) == false)
    {
        qDebug()<<"open config.json WriteOnly faild";
    }

    file.write(Jsondoc.toJson());

    file.close();
}

//将所有类型的json整合到根句柄中并写入文件
void CONFIG::updataJson_to_File()
{
    QFile file(filep);
    QJsonDocument Jsondoc;
    if(file.open(QIODevice::WriteOnly) == false)
    {
        qDebug()<<"open config.json WriteOnly faild";
    }

    configaddobj("TOOL_JSON",toolobj);
    configaddobj("HTTP_JSON",httpobj);
    configaddobj("AI_JSON",aiobj);

    Jsondoc.setObject(config);

    file.write(Jsondoc.toJson());

    file.close();
}

void CONFIG::setFile_Path(QString path)
{
    filep = path;
}

//将list转化为相应的json
void CONFIG::listToJson(JsonM type, QList<liststr> list)
{
    QString objname = "number";
    QJsonObject likeObject, *p = nullptr;

    if(type == TOOL_JSON)
    {
        p = &toolobj;
    }else if(type == HTTP_JSON)
    {
        p = &httpobj;
    }else if(type == AI_JSON)
    {
        p = &aiobj;
    }else{
        return;
    }

    p->insert("number",list.size());

    for(int i = 0;i < list.size();++i)
    {
        likeObject.insert("name", list.at(i).name);
        likeObject.insert("path", list.at(i).path);
        likeObject.insert("icon", list.at(i).icon);

        objname.append(QString::number(i));
        p->insert(objname,likeObject);
        objname = "number";
    }
}

//将输入的json添加到根句柄中
void CONFIG::configaddobj(QString name, QJsonObject Json)
{
    config.insert(name,Json);
}

//从完整的json文件中解析出信息相应类型的json句柄
QJsonObject CONFIG::get_Json(JsonM type)
{
    if(config.isEmpty() == true)
    {
        return QJsonObject();
    }

    if(type == TOOL_JSON)
    {
        return config.value("TOOL_JSON").toObject();
    }else if(type == HTTP_JSON)
    {
        return config.value("HTTP_JSON").toObject();
    }else if(type == AI_JSON)
    {
        return config.value("AI_JSON").toObject();
    }else{
        return QJsonObject();
    }
}

//将相应Json中的数据转化到list中
QList<liststr> CONFIG::get_list(JsonM type)
{
    QList<liststr> list;
    liststr onestr;

    QJsonObject typeJson = get_Json(type);
    QJsonObject conJson;
    QString str = "number";

    for(int i = 0;i < typeJson.value("number").toInt();++i)
    {
        str.append(QString::number(i));
        conJson = typeJson.value(str).toObject();

        onestr.name = conJson.value("name").toString();
        onestr.path = conJson.value("path").toString();
        onestr.icon = conJson.value("icon").toString();

        list.append(onestr);

        str = "number";
    }
    return list;
}

